package com.SouthernWall_404.age_tool.common.Network.Packets.S2C.ClientHandler;

import com.SouthernWall_404.age_tool.Client.Sound.MusicBlockIns;
import com.SouthernWall_404.age_tool.common.API.NBT.MusicBlockInfo;
import com.SouthernWall_404.age_tool.common.Network.Packets.S2C.MusicBlockStartPlayS2CPacket;
import com.SouthernWall_404.age_tool.common.world.Entity.MusicBlockEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.level.block.entity.BlockEntity;

import java.util.HashMap;
import java.util.Map;

public class MusicBlockStartPlayClientHandler {

    //private static MusicBlockInfo existingSetting=new MusicBlockInfo();

    private static Map<BlockPos, MusicBlockInfo> settingMap=new HashMap<>();
    private static Map<BlockPos,MusicBlockIns> insMap=new HashMap<>();


    public static void play(MusicBlockEntity musicBlockEntity,boolean trigger) {


        MusicBlockInfo currentSetting = musicBlockEntity.getMusicBlockSetting();//获取当前存储的设置
        SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(currentSetting.musicLocation);
        float volumn = currentSetting.volumn;
        int radius = currentSetting.radius;
        BlockPos blockPos = musicBlockEntity.getBlockPos();
        MusicBlockIns musicBlockIns = new MusicBlockIns(soundEvent, SoundSource.RECORDS, volumn, 1f, musicBlockEntity.getBlockPos(), radius);
//        Minecraft.getInstance().player.sendSystemMessage(Component.literal(""+insMap));

        if (!insMap.containsKey(blockPos)&&trigger)//没有播放
        {

            Minecraft.getInstance().getSoundManager().play(musicBlockIns);
            settingMap.put(blockPos, currentSetting);
            insMap.put(blockPos, musicBlockIns);
        } else {
            MusicBlockInfo existingSetting = settingMap.get(blockPos);
            if (!trigger) {
                MusicBlockIns existingIns = insMap.get(blockPos);
                Minecraft.getInstance().getSoundManager().stop(existingIns);
                settingMap.remove(blockPos);
                insMap.remove(blockPos);
                return;
            }

            if (!currentSetting.is(existingSetting))//若设置不同
            {
                MusicBlockIns existingIns = insMap.get(blockPos);
                Minecraft.getInstance().getSoundManager().stop(existingIns);
                Minecraft.getInstance().getSoundManager().play(musicBlockIns);
                settingMap.put(blockPos, currentSetting);
                insMap.put(blockPos, musicBlockIns);
            } else {
                return;
            }
        }
    }
    public static void play(MusicBlockStartPlayS2CPacket msg) {
        BlockEntity blockEntity=Minecraft.getInstance().level.getBlockEntity(msg.blockPos);
        if(blockEntity!=null&&blockEntity instanceof MusicBlockEntity musicBlockEntity)
        {
            play(musicBlockEntity,msg.trigger);
        }
    }
}
